﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;
using ZoneLine.Models.XWStations;

namespace ZoneLine.Stations.XWStation
{
    public abstract class XWStationBase : WorkStationDBase
    {
        public void Init(string ip, int port = 102, string startAddr = "DB55.0", string endUploadCodeAddr = "DB55.18.1", string endUploadDataAddr = "DB55.18.2", string allowWorkAddr = "DB55.20")
        {
            Ip = ip;
            Port = port;
            _startAddr = startAddr;
            _readLength = 126;

            EndUpLoadCodeAddr = endUploadCodeAddr;
            EndUpLoadDataAddr = endUploadDataAddr;
            AllowWorkAddr = allowWorkAddr;
            HeartToPlcAddr = "DB55.18.0";

            StationRecordData = new XWStationResultBase();
        }

        /// <summary>
        /// 触发打印
        /// </summary>
        public int TriggerPrint { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int HandSignalPrint { get; set; }


        private XWStationResultBase _stationRecordData;

        public XWStationResultBase StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }

        protected override void DealWithSignal(string name)
        {
            if (name == nameof(StartUploadCode))
            {
                //todo 上传了条码 这时候验证是否要工作
                if (StartUploadCode)
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位结论验证,条码信息是{StationRecordData.AbsScanCode}");
                    var validataRlt = Validata(CurrentStationName, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位的验证结果是{validataRlt}");
                    var writeRlt = WriteWorkActionToPlc(AllowWorkAddr, validataRlt);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位向下位机下发消息，下发{writeRlt}");
                    return;
                }
                else
                {
                    WriteData(EndUpLoadCodeAddr, false);
                    WriteData(AllowWorkAddr, 0);
                    return;
                }
            }

            if (name == nameof(StartUploadData))
            {
                if (StartUploadData)
                {
                    //todo 上传数据 这时候将数据记录到数据库中
                    RecordData();
                    WriteData(EndUpLoadDataAddr, true);
                }
                else
                {
                    WriteData(EndUpLoadDataAddr, false);
                }
            }

            if (name == nameof(TriggerPrint))
            {
                if (TriggerPrint == 1 && HandSignalPrint == 0)
                {
                    PrintLabel();
                }
                else
                {
                    //  WriteData(PrintRltAddr, 0);
                }
                return;
            }

        }

        protected override void DealData(byte[] content)
        {
            StationRecordData.AbsScanCode = GetPlcString(content, 26);
            StationRecordData.BushScanCode = GetPlcString(content, 58);
            StationRecordData.PartType = GetPlcString(content, 90);
            StationRecordData.Result = GetPlcString(content, 102);
            StationRecordData.PressPosition = _client.ByteTransform.TransSingle(content, 114);
            StationRecordData.PressForce = _client.ByteTransform.TransSingle(content, 118);
            StationRecordData.CycleTime = _client.ByteTransform.TransSingle(content, 122);

            TriggerPrint = _client.ByteTransform.TransInt16(content, 500);
            HandSignalPrint = _client.ByteTransform.TransInt16(content, 506);

            #region 物料信息
            StationRecordData.UpSupportBatchCode = GetPlcString(content, 150);
            StationRecordData.UpSupportBatchCodeCut = _client.ByteTransform.TransInt16(content, 310);

            StationRecordData.BumperBatchCode = GetPlcString(content, 182);
            StationRecordData.BumperBatchCodeCut = _client.ByteTransform.TransInt16(content, 312);

            StationRecordData.BearingBatchCode = GetPlcString(content, 214);
            StationRecordData.BearingBatchCodeCut = _client.ByteTransform.TransInt16(content, 314);

            StationRecordData.DustCoverBatchCode = GetPlcString(content, 246);
            StationRecordData.DustCoverBatchCodeCut = _client.ByteTransform.TransInt16(content, 316);

            StationRecordData.SpringPadBatchCode = GetPlcString(content, 278);
            StationRecordData.SpringPadBatchCodeCut = _client.ByteTransform.TransInt16(content, 318);
            #endregion
        }

        protected override WorkActionEnum Validata(string stationName, string code, string[] otherStations = null)
        {
            var currentStationName = stationName;
            var stationIndex = Cache.WorkStationValidationList.IndexOf(currentStationName);
            if (stationIndex == -1)
            {
                return WorkActionEnum.NoEnable;
            }
            LogWithUi(_logUiChannel, $"开始检查是否在本工位{currentStationName}结论");
            var currentResult = CheckCodeExistAndResult(currentStationName, code);
            LogWithUi(_logUiChannel, $"在本工位{currentStationName}工位检查{code}的结论是{currentResult}");
            if (currentResult == 1)
            {
                return WorkActionEnum.HasWorkInCurrent;
            }
            else
            {
                return WorkActionEnum.StartWork;
            }
        }

        /// <summary>
        /// 抽象方法打印标签
        /// </summary>
        protected abstract void PrintLabel();

        //$P000000001010900638$V0018000040$L25031901$Q500$O4500125969$S80$E$N64
        //$Pxxxx@Lxxxx$QxxxDyyyyMMddHHmmss
        /// <summary>
        /// 生成合件批次码
        /// </summary>
        /// <returns></returns>
        protected string CreateComponentScanCode(int qty)
        {
            string materialNumber = "";//合件物料固定写死
            string materialBatch = DateTime.Now.ToString("yyyyMMdd");//合件批次码固定写死
            switch (Cache.Model)
            {
                case ModelEnum.LX:
                    materialNumber = "000000000000000001";
                    break;
                case ModelEnum.SQ:
                    materialNumber = "000000000000000002";
                    break;
                case ModelEnum.BQKH:
                    materialNumber = "000000000000000003";
                    break;
                case ModelEnum.BQLH:
                    materialNumber = "000000000000000004";
                    break;
                case ModelEnum.WL:
                    materialNumber = "000000000000000005";
                    break;
                default:
                    materialNumber = "000000000000000000";
                    break;
            }
            return $"$P{materialNumber}$L{materialBatch}$Q{qty}$D{DateTime.Now.ToString("yyyyMMddHHmmss")}";
        }
    
    }
}
